package org.suanfaTranning.ST200_Chatpter1_LineTable_HashTable_String.array;

/**
 * @Auther: qingle
 * @Date: 2024/9/5-0:20
 * @Description:
 * @version: 1.0
 */
public class myatIo {

	public int myAtoi(String s) {
		String s1 = s.trim();
		int sign = 1;
		long result = 0L;
		if (s1.length() != 0 && s1.charAt(0) == '-') {
			sign = -1;
			// 原来的数据去掉负号
			s1 = s1.substring(1);
		}

		StringBuilder res = new StringBuilder();
		// two pointer
		int i = 0,j = 0;
		for(; i < s1.length(); i++) {
			j = i;
			while (s1.charAt(j) >= '0' && s1.charAt(j) <= '9') {
				j++;
			}
			j--;
		}
			res.append(s1, i, j);
			result = (long) sign * Integer.valueOf(res.toString());
			// 如果result已经大于MAX / 10，或者result等于MAX / 10且pop大于7，则乘以10后加上pop会超出正数最大值
			if (result >  Integer.MAX_VALUE) return 0;

			// 检查是否会导致负数溢出
			// 如果result已经小于MIN / 10，或者result等于MIN / 10且pop小于-8，则乘以10后加上pop会低于负数最小值
			if (result <  Integer.MIN_VALUE) return 0;
		return (int) result;
	}

	public static void main(String[] args) {

		myatIo myatIo = new myatIo();
		System.out.println(myatIo.myAtoi("-91283472332"));
	}
}
